﻿using InterLinq.Sebarf;
using InterLinq.Server.Services.Impl;
using InterLinq.Services.Interfaces;
using Sebarf.Configuration;
using Sebarf.Core;
using Sebarf.Server;
using Sebarf.Services.Impl;
using Sebastof.Server.Services.Impl;

namespace InterLinq.Base {
	public class InterLinqServer {
		public static IServer GetSebarfServerForInterLinq(IQueryHandler queryHandler, IBackendProvider backEndProvider,
														   string fileNameOfInternalRequestHandlerConfiguraion,
														   string fileNameOfApplicationRequestHandlerConfiguraion,
														   string ServiceURI) {
			IServer server = WcfBasedServer.Create(delegate {
				// create the base service
				var locator = new ServiceRegistrationLocatorService();
				// register backend locator service
				locator.RegisterService(new BackendProviderService());
				// register query handler locator service
				locator.RegisterService(new QueryHandlerLocatorService { QueryHandler = queryHandler });
				// create engine
				RequestHandlerEngine toReturn = RequestHandlerEngine.Create(locator,
																			 RequestHandlerLoader
																				.Load(
																				fileNameOfApplicationRequestHandlerConfiguraion),
																			 RequestHandlerLoader
																				.Load(
																				fileNameOfInternalRequestHandlerConfiguraion));
				// add interlinq handler
				locator.GetRegisteredService<IAddRequestHandlerService>().
					AddAddionalHandler(new ExecuteLinqQueryRequestHandler { QueryHandler = queryHandler });
				// set backend provider
				locator.GetRegisteredService<IBackendProviderRegisterService>().
					SetBackendProvider(backEndProvider);

				return toReturn;
			}, ServiceURI);

			return server;
		}
	}
}